<!-- 验证插件 -->
<script src="/static/plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="/static/plugins/jquery-validation/localization/messages_zh.min.js"></script>

<!-- 针对当前主题进行简单封装，精简代码 -->
<script src="/static/sdtheme/scripts/sdvalidate.js"></script>
<script src="/static/plugins/summer-note/js/summernote.min.js"></script>
<script src="/static/plugins/summer-note/js/summernote-zh-CN.js"></script>

<script>
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    $(function () {
       pageInit()
    });

    function pageInit() {
        validateInfo()
    }

    $('div.summernote').each(function () {
        var $this = $(this);
        var placeholder = $this.attr("placeholder") || '';
        var url = $this.attr("action") || '';
        $this.summernote({
            lang: 'zh-CN',
            placeholder: placeholder,
            minHeight: 300,
            dialogsFade: true,
            dialogsInBody: true,
            disableDragAndDrop: false,
            callbacks: {
                onImageUpload: function (files) {
                    var $files = $(files);
                    console.log(url);
                    $files.each(function () {
                        var file = this;
                        var data = new FormData();
                        data.append("file", file);

                        $.ajax({
                            data: data,
                            type: "POST",
                            url: url,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function (res) {
                                if (res.code === 0) {
                                    $this.summernote('insertImage', res.obj)
                                }
                            }
                        })
                    })
                }
            }
        });
    });

    function validateInfo() {
        var $thisForm = $('#thisForm');
        $thisForm.sdvalidate({
            rules: {
                Title: {
                    maxlength: 150,
                    required: true
                }
            },
            submitHandler: function (form) {
                var url = '{{ urlfor "CourseContentController.Edit" }}';
                var $content = $('#Content');
                $('.note-editable').each(function () {
                    var $this = $(this);
                    $content.val($this.html())
                });
                var params = $thisForm.serialize();
                $.sdpost(url, params, function (res) {
                    if (res.code === 0) {
                        parent.layer.msg(res.msg);
                        parent.refresh(res.obj)
                        parent.layer.close(index)
                    } else {
                        parent.layer.alert(res.msg, {icon: 2, title: '失败'})
                    }
                });
                return false
            }
        });
    }

    function FormSubmit() {
        $('#thisForm').submit();
    }
</script>